package com.ruoyi.report.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.report.domain.ReportAttend;
import com.ruoyi.report.domain.ReportBenefit;
import com.ruoyi.report.domain.ReportSalary;
import com.ruoyi.report.service.IReportAttendService;
import com.ruoyi.report.service.IReportBenefitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 保险金汇总 Controller
 *
 * @author ruoyi
 * @date 2022-09-26
 */
@Controller
@RequestMapping("/report/benefit")
public class ReportBenefitController extends BaseController {

    private String prefix = "report/benefit";

    @Autowired
    private IReportBenefitService reportBenefitService;

    @GetMapping()
    public String benefit(ModelMap mmap)
    {
        mmap.put("benefitMonth", DateUtils.getMonth());
        return prefix + "/benefit";
    }

    /**
     * 查询考勤统计
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReportBenefit reportBenefit)
    {
        List<ReportBenefit> list = new ArrayList<>();
        long count = reportBenefitService.count(Wrappers.lambdaQuery(ReportBenefit.class).eq(ReportBenefit::getBenefitMonth, reportBenefit.getBenefitMonth()));
        startPage();
        if(count == 0) {
            list = reportBenefitService.selectBenefitReportList(reportBenefit);
        } else {
            list = reportBenefitService.selectReportBenefitList(reportBenefit);
        }
        return getDataTable(list);
    }

    /**
     * 用户提醒标记已处理
     */
    @PostMapping( "/checkClock")
    @ResponseBody
    public AjaxResult checkClock(String reportMonth)
    {
        long count = reportBenefitService.count(Wrappers.lambdaQuery(ReportBenefit.class).eq(ReportBenefit::getBenefitMonth, reportMonth));
        if(count > 0) {
            return error();
        }
        return success();
    }

    /**
     * 用户提醒标记已处理
     */
    @RequiresPermissions("report:benefit:clock")
    @Log(title = "保险金统计", businessType = BusinessType.OTHER)
    @PostMapping( "/clockReport")
    @ResponseBody
    public AjaxResult clockReport(String reportMonth)
    {
        long count = reportBenefitService.count(Wrappers.lambdaQuery(ReportBenefit.class).eq(ReportBenefit::getBenefitMonth, reportMonth));
        if(count == 0) {
            reportBenefitService.clockReportBenefitList(reportMonth);
        } else {
            return error("当前月份保险金数据已锁定");
        }
        return success();
    }

    @GetMapping("/report")
    public String report(ModelMap mmap)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        mmap.put("startMonth", DateUtils.getYear() + "-01");
        mmap.put("endMonth", DateUtils.getMonth());
        return prefix + "/report";
    }

    /**
     * 查询考勤统计
     */
    @PostMapping("/reportList")
    @ResponseBody
    public TableDataInfo reportList(ReportBenefit reportBenefit)
    {
        startPage();
        List<ReportBenefit> list = reportBenefitService.selectReportBenefitContentList(reportBenefit);
        return getDataTable(list);
    }

    /**
     * 导出保险金内容列表
     */
    @Log(title = "保险金统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReportBenefit reportBenefit)
    {
        List<ReportBenefit> list = reportBenefitService.selectReportBenefitContentList(reportBenefit);
        ExcelUtil<ReportBenefit> util = new ExcelUtil<ReportBenefit>(ReportBenefit.class);
        return util.exportExcel(list, "保险金统计");
    }
}
